//
//  XFEmotionTabBar.m
//  Weibo
//
//  Created by Fay on 15/10/16.
//  Copyright (c) 2015年 Fay. All rights reserved.
//

#import "XFEmotionTabBar.h"
#import "XFEmotionTabBarButton.h"
@interface XFEmotionTabBar()
@property (nonatomic, weak) XFEmotionTabBarButton *selectedBtn;

@end

@implementation XFEmotionTabBar

-(id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self setupBtn:@"最近" buttonType:XFEmotionTabBarButtonTypeRecent];
        [self setupBtn:@"默认" buttonType:XFEmotionTabBarButtonTypeDefault];
        self.sendBtn = [self setupBtn:@"发送" buttonType:XFEmotionTabBarButtonTypeSend];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

/**
 *  创建一个按钮
 *
 *  @param title 按钮文字
 */
- (XFEmotionTabBarButton *)setupBtn:(NSString *)title buttonType:(XFEmotionTabBarButtonType)buttonType {
    
    XFEmotionTabBarButton *btn = [[XFEmotionTabBarButton alloc]init];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    btn.tag = buttonType;
    [btn setTitle:title forState:UIControlStateNormal];
    [self addSubview:btn];
    
    
    if (buttonType == XFEmotionTabBarButtonTypeDefault) {
        [self btnClick:btn];
    }
    if (buttonType == XFEmotionTabBarButtonTypeSend) {
        [btn setBackgroundImage: MHImageNamed(@"bg_message_send") forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setTitleColor:MHAlertTextColor forState:0];
        [btn setBackgroundImage: [UIImage imageWithColor:MHColorFromHexString(@"#007AFF")] forState:UIControlStateSelected];
    }else
    {
        [btn setBackgroundImage:[UIImage imageWithColor:MHColorFromHexString(@"#EDEDED")] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithColor:MHColorFromHexString(@"#FFFFFF")] forState:UIControlStateDisabled];
    }
    
    return btn;
    
}

//重写delegate 方法
-(void)setDelegate:(id<XFEmotionTabBarDelegate>)delegate {
    
    _delegate = delegate;
    
    //选中默认按钮
    [self btnClick:(XFEmotionTabBarButton *)[self viewWithTag:XFEmotionTabBarButtonTypeDefault]];
    
    
}


/**
 *  按钮点击
 */
- (void)btnClick:(XFEmotionTabBarButton *)btn {
    if (btn != self.sendBtn) {
        self.selectedBtn.enabled = YES;
        self.selectedBtn = btn;
    }
    btn.enabled = (self.sendBtn == btn);
    
    //通知代理
    if ([self.delegate respondsToSelector:@selector(emotionTabBar:didSelectButton:)]) {
        [self.delegate emotionTabBar:self didSelectButton:(int)btn.tag];
    }
    
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置按钮的frame
    NSUInteger btnCount = self.subviews.count;
    //    CGFloat btnW = self.width / btnCount;
    CGFloat btnH = self.height; //442
    for (int i = 0; i<btnCount; i++) {
        XFEmotionTabBarButton *btn = self.subviews[i];
        btn.y = 0;
        btn.width = (i == btnCount - 1) ? self.width * 0.2 : self.width * 0.4;
        btn.x = i * self.width * 0.4;
        btn.height = btnH;
    }
}


@end
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com
